package com.itcast.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.reggie.pojo.R;
import com.itcast.reggie.pojo.Setmeal;
import com.itcast.reggie.Dto.SetmealDto;
import com.itcast.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController{
    @Autowired
    private SetmealService setmealService;

    //根据姓名查询
    @GetMapping("/page")
    public R<Page> GetByPage(int page, int pageSize, String name) {
        Page page1 = new Page(page, pageSize);
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), "name", name);
        Page page2 = setmealService.page(page1, wrapper);
        return R.success(page2);
    }

    //修改套餐-第一阶段-数据回写
    @GetMapping("/{setmealId}")
    public R<SetmealDto> ReWrite(@PathVariable Long setmealId) {
        SetmealDto setmealDto = setmealService.ReWrite(setmealId);
        return R.success(setmealDto);
    }

    //修改套餐-第二阶段-修改
    @PutMapping
    public R<String> UpdateById(@RequestBody SetmealDto setmealDto){
        setmealService.UpdateById(setmealDto);
        return R.success("修改成功");
    }

    //添加套餐
    @PostMapping
    public R<String> AddSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.AddSetmeal(setmealDto);
        return R.success("添加成功");
    }

    //套餐起售,停售状态改变
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status,Long[] ids){
        setmealService.updateStatus(status, ids);
        return R.success("修改成功");
    }

    //套餐批量删除
    @DeleteMapping()
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> GetList(Long categoryId){
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }

}


